package com.kun.order.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.kun.common.utils.R;
import com.kun.order.entity.OrderEntity;
import com.kun.order.feign.ProductFeignService;
import com.kun.order.vo.ClassifyVo;
import com.kun.order.vo.OrderDetailedVo;
import com.kun.order.vo.ProductVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kun.common.utils.PageUtils;
import com.kun.common.utils.Query;

import com.kun.order.dao.OrderDetailedDao;
import com.kun.order.entity.OrderDetailedEntity;
import com.kun.order.service.OrderDetailedService;


@Service("orderDetailedService")
public class OrderDetailedServiceImpl extends ServiceImpl<OrderDetailedDao, OrderDetailedEntity> implements OrderDetailedService {
    @Autowired
    ProductFeignService productFeignService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderDetailedEntity> page = this.page(
                new Query<OrderDetailedEntity>().getPage(params),
                new QueryWrapper<OrderDetailedEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<OrderDetailedVo> getOrderDestailByOederId(String id) {
        Map<Integer, String> map = new HashMap<>();
        R r = productFeignService.getAllClassify();
        TypeReference<List<ClassifyVo>> typeReference = new TypeReference<List<ClassifyVo>>() {
        };//声明内部类，指明类型
        List<ClassifyVo> classifyVoList =  r.getData(typeReference);
        System.out.println(classifyVoList.size());
        for (ClassifyVo classifyVo:classifyVoList){
            map.put(classifyVo.getId(),classifyVo.getClassify());
        }
        QueryWrapper<OrderDetailedEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parentid",id);
        List<OrderDetailedEntity> orderDetailedEntities = baseMapper.selectList(queryWrapper);
        List<OrderDetailedVo> collect = orderDetailedEntities.stream().map((orderDetailedEntity) -> {
            OrderDetailedVo orderDetailedVo = new OrderDetailedVo();
            BeanUtils.copyProperties(orderDetailedEntity, orderDetailedVo);
            String classify = map.get(orderDetailedEntity.getCid());

            orderDetailedVo.setClassify(classify);
            return orderDetailedVo;
        }).collect(Collectors.toList());
        return  collect;

    }

    @Override
    public void addOrderDetailed(OrderDetailedEntity orderDetailed) {

        Integer sid = orderDetailed.getSid();
        R info = productFeignService.info(sid.toString());
        ProductVo productVo = info.getData("product",new TypeReference<ProductVo>() {
        });
        orderDetailed.setPName(productVo.getProductName());
        orderDetailed.setPUnitName(productVo.getUnit());
        orderDetailed.setPUnitPrice(productVo.getUnitPrice());
        orderDetailed.setCid(Integer.parseInt(productVo.getCid()));
        baseMapper.insert(orderDetailed);
    }

    @Override
    public void updateDetailed(OrderDetailedEntity orderDetailed) {
        Integer sid = orderDetailed.getSid();
        R info = productFeignService.info(sid.toString());
        ProductVo productVo = info.getData("product",new TypeReference<ProductVo>() {
        });
        orderDetailed.setPName(productVo.getProductName());
        orderDetailed.setPUnitName(productVo.getUnit());
        orderDetailed.setPUnitPrice(productVo.getUnitPrice());
        orderDetailed.setCid(Integer.parseInt(productVo.getCid()));
        baseMapper.updateById(orderDetailed);
    }

}